<template>
  <div class="addLabel">
    <div class="sta_title">
      <div class="goBack" @click="save">
        <Icon type="ios-arrow-back"/>
        <span class="gb_font">返回</span></div>
      <div class="st_font">
        <span class="stf_title">添加标签</span>
      </div>
    </div>
    <div class="content">
      <div class="con_top">
        <div class="title">已有标签</div>
        <div class="labels">
          <div class="label" v-for="item in labelList">{{item.title}}<Icon type="ios-close-circle" @click="deleteLabel(item)" /></div>
        </div>
      </div>
      <div class="con_bottom">
        <div class="title">添加标签</div>
        <div class="inputs">
          <Input v-model="newLabel" placeholder="请输入标签内容" style="width: 250px" />
          <Button type="primary" @click="addOneLabel">添加</Button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import cookie from 'js-cookie';
  export default {
    name: "addLabel",
    props: ['labelData'],
    data() {
      return {
        labelList: [],
        newLabel: '',
      }
    },
    created() {
      let self = this
      this.getInfo()
    },
    methods: {
      save() {
        let self = this
        this.$emit('save',1)
      },
      getInfo(){
        let self = this
        this.$Loading.start();
        var str = {
          problem_id: self.labelData,
          uid: cookie.get('uid'),
          loginType: cookie.get('loginType')
        };
        var strdata = this.$twjsdes.enstr(str);
        this.$http.post(this.$API_URL + '/item/labelchoice', {alldata: strdata.alldata}).then((response) => {
          var res = this.$twjsdes.decrypt(response.data.alldata);
          self.labelList = res.data.list
          this.$Loading.finish();
        }).catch(function (error) {
          this.$Modal.error({
            title: '提示',
            content: '网络连接错误，请检查后重试'
          });
          //console.log(error);
        });
      },
      addOneLabel(){
        let self = this
        if (self.newLabel != ''){
          this.$Loading.start();
          var str = {
            problem_id: self.labelData,
            title: self.newLabel,
            uid: cookie.get('uid'),
            loginType: cookie.get('loginType')
          };
          var strdata = this.$twjsdes.enstr(str);
          this.$http.post(this.$API_URL + '/item/labeladd', {alldata: strdata.alldata}).then((response) => {
            var res = this.$twjsdes.decrypt(response.data.alldata);
            self.getInfo()
            self.newLabel = ''
            this.$Loading.finish();
          }).catch(function (error) {
            this.$Modal.error({
              title: '提示',
              content: '网络连接错误，请检查后重试'
            });
            //console.log(error);
          });
        }
      },
      deleteLabel(item){
        let self = this
        this.$Loading.start();
        var str = {
          problem_id: item.problem_id,
          problem_label_id: item.problem_label_id,
          uid: cookie.get('uid'),
          loginType: cookie.get('loginType')
        };
        var strdata = this.$twjsdes.enstr(str);
        this.$http.post(this.$API_URL + '/item/labeldel', {alldata: strdata.alldata}).then((response) => {
          var res = this.$twjsdes.decrypt(response.data.alldata);
          self.getInfo()
          this.$Loading.finish();
        }).catch(function (error) {
          this.$Modal.error({
            title: '提示',
            content: '网络连接错误，请检查后重试'
          });
          //console.log(error);
        });
      }
    }
  }
</script>

<style scoped lang="less">
  .addLabel {
    width: 79%;
    height: 100%;
    display: flex;
    flex-direction: column;
    position: fixed;
    right: 0;
    top: 0;
    opacity: 10;
    background: #f4f4f4;
    padding: 0 16px;
    box-sizing: border-box;
    .sta_title {
      display: flex;
      flex-direction: row;
      justify-content: center;
      position: relative;
      margin: 16px 0;
      width: 100%;
      height: 36px;
      line-height: 36px;
      .goBack {
        display: flex;
        flex-direction: row;
        position: absolute;
        left: 0;
        top: 0;
        cursor: pointer;
        .ivu-icon {
          font-size: 20px;
          line-height: 36px;
        }
        .gb_font {
          font-size: 17px;
          color: #666666;
          line-height: 37px;
        }
      }
      .st_font {
        display: flex;
        flex-direction: row;
        .stf_title {
          font-size: 17px;
          color: #222222;
          margin-left: 50px;
          margin-right: 50px;
        }
      }
    }
    .content{
      width: 100%;
      background: #fff;
      border-radius: 5px;
      border: 1px solid #e1e1e1;
      display: flex;
      flex-direction: column;
      padding: 16px;
      box-sizing: border-box;
      .con_top,.con_bottom{
        display: flex;
        flex-direction: column;
        .title{
          font-size: 13px;
          color: #666666;
          line-height: 13px;
          margin-bottom: 10px;
        }
      }
      .con_top{
        padding-bottom: 8px;
        border-bottom: 1px dashed #e5e5e5;
        .labels{
          display: flex;
          flex-direction: row;
          flex-wrap: wrap;
          .label{
            height: 30px;
            line-height: 30px;
            padding: 0 15px;
            background: #f9f9f9;
            border: 1px solid #e5e5e5;
            border-radius: 3px;
            font-size: 14px;
            color: #222222;
            position: relative;
            margin-right: 10px;
            margin-bottom: 8px;
            .ivu-icon{
              position: absolute;
              top: -8px;
              right: -7px;
              color: #f15c60;
              font-size: 18px;
              cursor: pointer;
            }
          }
        }
      }
      .con_bottom{
        padding-top: 16px;
        button{
          margin-left: 10px;
          height: 35px;
          line-height: 35px;
          padding-top: 0;
          padding-bottom: 0;
        }
      }
    }
  }
  /* 手机竖屏 */
  @media screen and (min-width: 0px) and (max-width: 820px) {
    .addLabel{
      width: 100%;
      padding: 0 8px;
      .sta_title{
        margin: 8px 0;
      }
      .content{
        padding: 8px;
        .con_bottom{
          padding-top: 8px;
          .title{
            margin-bottom: 5px;
          }
        }
      }
    }
  }
</style>